﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Geodatabase;
using System.Reflection;
using ESRI.ArcGIS.esriSystem;
using System.IO;
using ESRI.ArcGIS.DataSourcesGDB;
using System.Windows.Forms;
using PlanningGIS.Win.Controls;
using PlanningGIS.Util.Convertor;
using PlanningGIS.Util.Log;

namespace PlanningGIS.DataConvert.model
{
    /// <summary>
    /// 作用：字段类型信息结构体,用来存储待创建要素类字段信息的结构体
    /// </summary>
    public struct FieldInfoType
    {
        /// <summary>
        /// 字段别名或描述
        /// </summary>
        public string aliasName;

        /// <summary>
        /// 字段名称
        /// </summary>
        public string fieldName;

        /// <summary>
        /// 字段类型
        /// </summary>
        public esriFieldType fieldType;

        /// <summary>
        /// 是否允许为空
        /// </summary>
        public bool isNullable;

        /// <summary>
        /// 字符串长度
        /// </summary>
        public int length;

        /// <summary>
        /// 精度
        /// </summary>
        public int precision;

        /// <summary>
        /// 小数点位数
        /// </summary>
        public int scale;
    }

    /// <summary>
    /// 作用：数据转换通用函数
    /// </summary>
    public static class DataConvertUtility
    {
        /// <summary>
        /// 根据XML文件节点打开工作区
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="input"></param>
        /// <returns></returns>
        public static IWorkspace GetWorkSpaceByObject<T>(T input, bool isCreate)
        {
            Type type = input.GetType();
            System.Reflection.FieldInfo[] fieldInfos = type.GetFields();
            System.Reflection.PropertyInfo[] proinfos = type.GetProperties();
            string wkstype = "";
            string path = "";
            string user = "";
            string psd = "";
            string server = "";
            string db = "";
            if (fieldInfos != null && fieldInfos.Length > 0)
            {
                foreach (FieldInfo info in fieldInfos)
                {
                    object obj = info.GetValue(input);
                    string tmp = obj == null ? "" : obj.ToString().Trim();
                    switch (info.Name)
                    {
                        case "WorkspaceType":
                            wkstype = tmp;
                            break;
                        case "Database":
                            db = tmp;
                            break;
                        case "Server":
                            server = tmp;
                            break;
                        case "User":
                            user = tmp;
                            break;
                        case "Password":
                            psd = tmp;
                            break;
                        case "Path":
                            path = tmp;
                            break;
                    }
                }
            }
            if (proinfos != null && proinfos.Length > 0)
            {
                foreach (PropertyInfo info in proinfos)
                {
                    object value = info.GetValue(input, null);
                    string tmp = value == null ? "" : value.ToString().Trim();
                    switch (info.Name)
                    {
                        case "WorkspaceType":
                            wkstype = tmp;
                            break;
                        case "Database":
                            db = tmp;
                            break;
                        case "Server":
                            server = tmp;
                            break;
                        case "User":
                            user = tmp;
                            break;
                        case "Password":
                            psd = tmp;
                            break;
                        case "Path":
                            path = tmp;
                            break;
                    }
                }
            }
            IWorkspace pWs = null;    
            try
            {
                switch (wkstype.ToUpper())
                {
                    case "SDE":
                        pWs = PlanningGIS.ArcGIS.WorkspaceHelper.OpenSDEWorkspace(server, user, psd, db);
                        break;
                    default:
                        if (!Directory.Exists(path.Substring(0, path.LastIndexOf('\\'))) || (!path.ToLower().EndsWith(".gdb") && !path.ToLower().EndsWith(".mdb"))) 
                        {
                            throw new Exception("输入路径有误!");
                        }
                        pWs = PlanningGIS.ArcGIS.WorkspaceHelper.CreateOrOpenWorksapce(path);
                        break;
                }
                return pWs;
            }
            catch (Exception ex)
            {
                LogHelper.WriteErrLog("创建或打开库体失败：" + ex.Message);
                return null;
            }
        }

    }
}
